/*
 * @Author: your name
 * @Date: 2021-11-12 21:33:25
 * @LastEditTime: 2021-11-29 11:01:47
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \li-yuwei\serve\app\service\article.js
 */
/*
 * @Author: your name
 * @Date: 2021-11-10 08:34:30
 * @LastEditTime: 2021-11-11 09:09:44
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \li-yuwei\egg-example\app\service\login.js
 */

const Service = require("egg").Service;
class ClassifyService extends Service {
    //获取分类
    async getClassifyList() {
            const { ctx, app } = this;
            const res = await app.mysql.select("classify");
            return res;
        }
        //获取分类个数
    async getClassifyTotal() {
            const { ctx, app } = this;
            const res = await app.mysql.select("classify");
            const total = res.length;
            return total;
        }
        //添加分类
    async addClassifyList() {
            const { ctx, app } = this;
            const { text, value } = ctx.request.body;
            //判断是否存在
            const isHaveText = await app.mysql.get("classify", { text });
            const isHaveValue = await app.mysql.get("classify", { value });
            //提示拒绝添加
            if (isHaveText) {
                return {
                    isHave: true,
                    message: "重复的分类名",
                };
            } else if (isHaveValue) {
                return {
                    isHave: true,
                    message: "重复的分类值",
                };
            } else {
                const res = await app.mysql.insert("classify", { text, value });
                return res;
            }
        }
        //删除分类
    async delClassifyList() {
        const { ctx, app } = this;
        const { id } = ctx.request.body;
        const res = await app.mysql.delete("classify", {
            id,
        });
        return res;
    }
    async editClassifyList() {
        const { ctx, app } = this;
        const { id, value, text } = ctx.request.body;
        const isHaveText = await app.mysql.get("classify", { text }); //是否存在同类text值
        const isHaveValue = await app.mysql.get("classify", { value }); //是否存在同类value值
        //判断是否有同value
        if (isHaveValue) {
            //判断id是否与修改id相同
            if (isHaveValue.id !== id) {
                return {
                    isHave: true,
                    message: "分类值已被占用",
                };
            } else {
                //判断是否有同text值
                if (isHaveText) {
                    //判断id是否与修改id相同
                    if (isHaveText.id !== id) {
                        return {
                            isHave: true,
                            message: "分类名已被占用",
                        };
                    }
                }
            }
        }
        //判断是否有同text值
        if (isHaveText) {
            //判断id是否与修改id相同
            if (isHaveText.id !== id) {
                return {
                    isHave: true,
                    message: "分类名已被占用",
                };
            } else {
                //判断是否有同value
                if (isHaveValue) {
                    //判断id是否与修改id相同
                    if (isHaveValue.id !== id) {
                        return {
                            isHave: true,
                            message: "分类值已被占用",
                        };
                    }
                }
            }
        }
        //否则更新
        const res = await app.mysql.update("classify", {
            id,
            value,
            text,
        });
        return res;
    }

    // **********用户端*************//
    //文章分类数据
    async getClassifyArticle() {
            const { ctx, app } = this;
            const { text } = ctx.params;
            const article = await app.mysql.select("allarticle");
            let res = [],
                result = [];
            article.forEach((item) => {
                if (item.state * 1) {
                    res.push(item);
                }
            });
            res.sort((a, b) => {
                return (
                    new Date(b.release_time).getTime() - new Date(a.release_time).getTime()
                );
            });
            res.forEach((item) => {
                if (item.classify === text) {
                    result.push(item);
                }
            });
            return result;
        }
        //获取分类个数
    async getClassifyNum() {
        const { ctx, app } = this;
        let classify = await app.mysql.select("classify");
        const article = await app.mysql.select("allarticle");
        classify.forEach((item, index) => {
            article.forEach((val) => {
                if (val.state * 1) {
                    if (item.text === val.classify) {
                        if (classify[index].num) {
                            classify[index].num = classify[index].num + 1;
                        } else {
                            classify[index].num = 1;
                        }
                    }
                }
            });
        });
        return classify;
    }
}
module.exports = ClassifyService;